<template>
  <el-dialog :destroy-on-close="true"   :lock-scroll="false" title="退回" :visible.sync="visible" width="750px"
             :before-close="cancel">
    <el-form ref="form" :model="form" label-width="100px" :rules="rules" >
      <el-form-item label="申请人：">
        <el-input v-model="form.number" placeholder="自动带入" disabled/>
      </el-form-item>
      <el-form-item label="名称：">
        <el-input v-model="form.name" placeholder="自动带入" disabled/>
      </el-form-item>

      <el-form-item label="类别：" >
        <el-input v-model="form.part_category_name" placeholder="自动带入" disabled/>
      </el-form-item>

      <el-form-item label="规格型号：">
        <el-input v-model="form.part_category_model" placeholder="自动带入" disabled/>
      </el-form-item>
      <el-form-item label="计量单位：">
        <el-input v-model="form.part_category_uom" placeholder="自动带入" disabled/>
      </el-form-item>
      <el-form-item label="目标仓库：" prop="target_warehouse_id">
        <WarehouseSelect :astrict="0" v-model="form.target_warehouse_id" placeholder="请选择目标出库"/>
      </el-form-item>
      <el-form-item label="个人持有：">
        <el-input v-model="form.quantity" placeholder="自动带入" disabled/>
      </el-form-item>

      <el-form-item label="退回数量：" prop="quantity">
          <el-input-number v-model="quantity"  type="number" :min="1" :max="form.quantity-form.disabled_inventory" />
      </el-form-item>

      <el-form-item label="说明：">
        <el-input v-model="description" placeholder="请输入备注" type="textarea" :rows="5"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import WarehouseSelect from "@/views/bom/stock/components/WarehouseSelect.vue";
export default {
  components: {WarehouseSelect},
  props: {
    // 修改回显的数据
    data: Object,
    typeList:Array
  },

  data() {
    const validateQuantity = (rule, value, callback) => {
      if (this.quantity === '') {
        callback(new Error('请输入数量'));
      } else {
        callback();
      }
    };
    return {
      editNum: null,
      // 弹窗是否打开
      visible: false,
      loading: false,
      quantity:1,
      id:null,
      // 表单数据
      description:'',
      form: {
      },
      rules: {
        quantity: [
          {required: true, validator: validateQuantity,trigger: 'change'},
        ],
        type:[
          {required: true, message: '请选择入库类型', trigger: 'blur'},
        ],
        target_warehouse_id:[
          {required: true, message: '请选择目标仓库', trigger: 'change'},
        ],
      },
    }
  },
  watch:{

  },
  methods: {
    active(info) {
      this.visible = true;
      //请求接口
      this.form={...info}
      this.form.description=''
    },
    cancel() {
      this.description=''
      this.visible = false;
      this.loading=false
      this.quantity=1
      this.form={
        part_categories:[]
      }
    },
    /*保存*/
    save(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // let part_categories=[]
          // part_categories.push({id:this.form.id,quantity:this.quantity})
          // this.form.part_categories=part_categories
          this.loading=true
          this.$http.post(`/user_inventories/${this.form.id}/back`,{quantity:this.quantity,description:this.description,target_warehouse_id:this.form.target_warehouse_id}).then((res) => {
            if(res.data.code===0){
              this.$message.success(res.data.msg)
              this.$emit('reload')
            }else {
              this.$message.error(res.data.msg)
            }
            this.cancel()
          })
          /*发起请求*/
          this.cancel()
        } else {
          return false;
        }
      });
    },

  },
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
